<script lang="ts" setup name="XtxCarousel">
import { BannerResult } from '@/types/data'
import { onBeforeUnmount, onMounted, ref } from 'vue'
// import { PropType } from 'vue'
// 如果以后注册组件使用不成功 记得在script中写点东西
// vue3的父传子接收的方式  没有类型校验
/* defineProps({
  slides: {
    type: Array,
    required: true
  }
}) */
// 有类型校验  需要配合 PropType  比较麻烦了解
/* defineProps({
  slides: {
    type: Array as PropType<BannerResult[]>,
    required: true
  },
  autoplay: {
    type: Boolean,
    default: false
  },
  duration: {
    type: Number,
    default: 3000
  },
  isShowInd: {
    type: Boolean,
    default: false
  }
}) */
interface IProps {
  slides: BannerResult[]
  autoplay?: boolean
  duration?: number,
  isShowInd?: boolean,
  height?: number
}
// v3 + ts  方便快捷
const { slides, autoplay = false, duration = 3000, isShowInd = false, height = 500 } = defineProps<IProps>()
// 激活的索引
const activeIndex = ref(0)
// 上一张
const pre = () => {
  activeIndex.value--
  // 到了第一张从最后一张继续
  if (activeIndex.value < 0) {
    activeIndex.value = slides.length - 1
  }
}
// 下一张
const next = () => {
  activeIndex.value++
  // 到了最后一张从第一张继续
  // if (activeIndex.value >= props.slides.length)
  if (activeIndex.value > slides.length - 1) {
    activeIndex.value = 0
  }
}
// 在DOM挂载结束后, 开启一个定时器, 开启自动轮播
let timerId: number = -1
onMounted(() => {
  // 如果进入页面就禁用自动轮播 (只禁止轮播一次, 鼠标只要经过后还会轮播)
  // if (!autoplay) return
  start()
})
// 组件销毁的时候清除定时器
onBeforeUnmount(() => {
  stop()
})
// 鼠标进入暂停轮播
const stop = () => {
  window.clearInterval(timerId)
}
// 鼠标离开开启轮播
const start = () => {
  if (!autoplay) return // 如果没有传递autoplay, 不会自动轮播
  clearInterval(timerId) // 优化代码
  timerId = window.setInterval(() => {
    // console.log('定时器在运行')
    next()
  }, duration)
}
</script>

<template>
  <div class="xtx-carousel" @mouseenter="stop" @mouseleave="start" :style="{ height: height + 'px' }">
    <ul class="carousel-body">
      <li class="carousel-item" :class="{ fade: activeIndex === index }" v-for="(item, index) in slides" :key="item.id">
        <RouterLink to="/">
          <img :src="item.imgUrl" alt="" />
        </RouterLink>
      </li>
    </ul>
    <a href="javascript:;" class="carousel-btn prev" @click="pre"><i class="iconfont icon-angle-left"></i></a>
    <a href="javascript:;" class="carousel-btn next" @click="next"><i class="iconfont icon-angle-right"></i></a>
    <div class="carousel-indicator" v-if="isShowInd">
      <span @mouseenter="activeIndex = index" :class="{ active: activeIndex === index }" v-for="(item, index) in slides"
        :key="item.id"></span>
    </div>
  </div>
</template>

<style scoped lang="less">
.xtx-carousel {
  width: 100%;
  height: 100%;
  min-width: 300px;
  min-height: 150px;
  position: relative;

  .carousel {
    &-body {
      width: 100%;
      height: 100%;
    }

    &-item {
      width: 100%;
      height: 100%;
      position: absolute;
      left: 0;
      top: 0;
      opacity: 0;
      transition: opacity 0.5s linear;

      &.fade {
        opacity: 1;
        z-index: 1;
      }

      img {
        width: 100%;
        height: 100%;
      }
    }

    &-indicator {
      position: absolute;
      left: 0;
      bottom: 20px;
      z-index: 2;
      width: 100%;
      text-align: center;

      span {
        display: inline-block;
        width: 12px;
        height: 12px;
        background: rgba(0, 0, 0, 0.2);
        border-radius: 50%;
        cursor: pointer;

        ~span {
          margin-left: 12px;
        }

        &.active {
          background: #fff;
        }
      }
    }

    &-btn {
      width: 44px;
      height: 44px;
      background: rgba(0, 0, 0, 0.2);
      color: #fff;
      border-radius: 50%;
      position: absolute;
      top: 228px;
      z-index: 2;
      text-align: center;
      line-height: 44px;
      opacity: 0;
      transition: all 0.5s;

      &.prev {
        left: 20px;
      }

      &.next {
        right: 20px;
      }
    }
  }

  &:hover {
    .carousel-btn {
      opacity: 1;
    }
  }
}
</style>